package com.badlogic.androidgames;

import com.badlogic.androidgames.FontTest.RenderView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class SurfaceViewTest extends Activity {
	FastRenderView renderView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		renderView = new FastRenderView(this);
		setContentView(renderView);
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		renderView.pause();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		Log.d("SurfaceViewText" , "sono nel metodo Onresume");
		super.onResume();
		renderView.resume();
	}
	
	class FastRenderView extends SurfaceView implements Runnable{

		Thread renderThread = null;
		SurfaceHolder holder;
		volatile boolean running = false;
		
		public FastRenderView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			holder = getHolder();
		}

		public void resume() {
			// TODO Auto-generated method stub
			running = true;
			renderThread = new Thread(this);
			Log.d("SurfaceViewText" , "sono nel metodo resume");
			renderThread.start();
			
		}

		public void pause() {
			// TODO Auto-generated method stub
			running = false;
			while(true) {
				try {
					renderThread.join();
				} catch (InterruptedException e) {
					// retry
				}
			}
			
		}
		public void run() {
			// TODO Auto-generated method stub
			while(running){
				if(holder.getSurface().isValid()){
					Canvas canvas = holder.lockCanvas();
					canvas.drawRGB(255, 0, 0);
					holder.unlockCanvasAndPost(canvas);
					
				}		
					
			}
			
		}
		
		
		
	}
}
